﻿using System.ComponentModel;
using System.Linq;
using System.Web.UI;

namespace Utils.UIPermission
{
    /// <summary>
    /// 
    /// </summary>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:CoolitePermitGridCommand runat=server></{0}:CoolitePermitGridCommand>")]
    public class CoolitePermitGridCommand : Coolite.Ext.Web.GridCommand
    {
        /// <summary>
        /// Gets or sets the name of the permission.
        /// </summary>
        /// <value>
        /// The name of the permission.
        /// </value>
        public string PermissionName
        {
            set
            {
                this.ViewState["__Name"] = value;
                UpdateDisplay(HideAsDisabled);
            }
            get
            {
                return this.ViewState["__Name"] as string;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [hide as disabled].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [hide as disabled]; otherwise, <c>false</c>.
        /// </value>
        public bool HideAsDisabled
        {
            set
            {
                this.ViewState["__HideAsDisabled"] = value;
                UpdateDisplay(value);
            }
            get
            {
                object obj = this.ViewState["__HideAsDisabled"];
                return (obj == null ? false : bool.Parse(obj.ToString()));
            }
        }

        /// <summary>
        /// Updates the display.
        /// </summary>
        /// <param name="newHideAsDisabledValue">if set to <c>true</c> [new hide as disabled value].</param>
        public void UpdateDisplay(bool newHideAsDisabledValue)
        {
            if (!string.IsNullOrEmpty(PermissionName))
            {
                bool bCanshow = UIPermissions.GetPermissions().Contains(PermissionName);
                if (!newHideAsDisabledValue)
                {
                    this.Disabled = false;
                    this.Hidden = !bCanshow;
                }
                else
                {
                    this.Hidden = false;
                    this.Disabled = !bCanshow;
                }
            }
        }
    }


}
